package com.example.user3.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.example.user3.demo.contract.MainContract;
import com.example.user3.demo.fragment.ItemFragment;
import com.example.user3.demo.mvp.presenter.MainPresenter;
import com.example.user3.demo.utils.ScaleTransitionPagerTitleView;
import com.example.user3.demo.wiget.MySwipeView;
import com.example.user3.demo.wiget.SwipeToFinishView;
import com.zzy.base.activity.BaseActivity;
import com.zzy.base.widget.NoScrollViewPager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = "/app/test2")
public class TestActivity2 extends BaseActivity<MainContract.IPresenter> implements MainContract.IView {


    public static void lauchActivityAnim(Context context) {
        Intent intent = new Intent(context, TestActivity2.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(intent);
        ((Activity)context).overridePendingTransition(R.anim.activity_anim_in,
                0);

    }

    @Autowired(name= "flag")
    boolean flag;


    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.uc_viewpager)
    NoScrollViewPager mViewPager;
    private MyPagerAdapter mAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);

        ARouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test2;
    }
    SwipeToFinishView swipeToFinishView;
    MySwipeView swipeView;
    @Override
    protected void initViews() {
//        MagicIndicator
        swipeView= new MySwipeView(this);
        swipeView.setActivity(TestActivity2.this);
        magicIndicator =findViewById(R.id.magic_indicator);
        mViewPager =findViewById(R.id.uc_viewpager);

        mFragments.add(ItemFragment.getUserInfoFragment("0"));
        mFragments.add(ItemFragment.getUserInfoFragment(""));


        CommonNavigator commonNavigator = new CommonNavigator(TestActivity2.this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(viewpagerAdapter);
        magicIndicator.setNavigator(commonNavigator);

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });

        mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
        swipeView.setFirstPage(true);
//        mViewPager.setNoScroll(true);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
                Log.i("zzy","onPageScrolled"+"===="+positionOffset+"==="+positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    swipeView.setFirstPage(true);
                }else{
                    swipeView.setFirstPage(false);
                }

                magicIndicator.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i("zzy","onPageScrollStateChanged");
                magicIndicator.onPageScrollStateChanged(state);
            }
        });

        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            float startX;
            float startY;
            float endX;
            float endY;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX=event.getX();
                        startY=event.getY();
                        Log.i("zzy","startX"+startX);
                        break;
                    case MotionEvent.ACTION_UP:
//                        endX=event.getX();
//                        endY=event.getY();
//                        WindowManager windowManager= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
//                        //获取屏幕的宽度
//                        Point size = new Point();
//                        windowManager.getDefaultDisplay().getSize(size);
//                        int width=size.x;
//                        //首先要确定的是，是否到了最后一页，然后判断是否向左滑动，并且滑动距离是否符合，我这里的判断距离是屏幕宽度的4分之一（这里可以适当控制）
//                        if(mViewPager.getCurrentItem()==0&&startX-endX<0&&endX-startX>=(width/4)){
//                            swipeView.collapse();
//                        }
                        break;
                }
                return false;
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected MainContract.IPresenter setPresenter() {
        return new MainPresenter() ;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void success(String content) {

    }



    private List<String> mDataList = Arrays.asList("同城秒杀", "免费换购");
    CommonNavigatorAdapter viewpagerAdapter =new CommonNavigatorAdapter() {
        @Override
        public int getCount() {
            return mDataList == null ? 0 : mDataList.size();
        }

        @Override
        public IPagerTitleView getTitleView(Context context, final int index) {
            ScaleTransitionPagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
            simplePagerTitleView.setText(mDataList.get(index));
            simplePagerTitleView.setSelectBig(false);
            simplePagerTitleView.setTextSize(15);
            simplePagerTitleView.setGracityType(3);
            simplePagerTitleView.setNormalColor(Color.parseColor("#00ff00"));
            simplePagerTitleView.setSelectedColor(Color.parseColor("#ff0000"));
            simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(index);
                }
            });
            return simplePagerTitleView;
        }

        @Override
        public IPagerIndicator getIndicator(Context context) {
            LinePagerIndicator indicator = new LinePagerIndicator(context);
            indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
            indicator.setLineHeight(UIUtil.dip2px(context, 1));
            indicator.setLineWidth(UIUtil.dip2px(context, 60));
            indicator.setRoundRadius(UIUtil.dip2px(context, 1));
            indicator.setStartInterpolator(new AccelerateInterpolator());
            indicator.setEndInterpolator(new DecelerateInterpolator(1.0f));
//            indicator.setColors(getResources().getColor(R.color.app_theme_color));
            return indicator;
        }
    };

    private class MyPagerAdapter extends FragmentPagerAdapter {

        MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }

}
